@extends('layouts.default')
@section('content')
    <div class="container">
        <div class="row row-display row-centered">
            <div class="col-md-9 col-centered">
                <!-- /.box-header -->
                <!-- form start -->
                <form role="form" action="{{route('permissions.batchstore')}}" method="post">
                    {{ csrf_field() }}
                    <div class="box-body">
                        <div class="form-group">
                            <label>所属菜单</label>
                            <select class="form-control" name="pid">
                                <option value="0"> /</option>
                                @if($permissionstree)
                                    @foreach($permissionstree as $menu)
                                        <option value="{{ $menu->id }}">{{ $menu->display_name }}</option>
                                    @endforeach
                                @endif
                            </select>
                        </div>
                        <div class="form-group">
                            <table class="table table-bordered">
                                <thead>
                                <tr>
                                    <th>名称</th>
                                    <th>路由</th>
                                    <th>是否显示</th>
                                    <th></th>
                                </tr>
                                </thead>
                                <tbody>
                                <tr v-for="(obj,index) in list">
                                    <td>
                                        <input type="text"  class="form-control" name="display_name[]" required>
                                    </td>
                                    <td>
                                        <select class="form-control"  name="name[]" data-live-search="true">
                                            <option value="">请选择</option>
                                            @if($routename)
                                                @foreach($routename as $name)
                                                    <option value="{{ $name }}">{{ $name }}</option>
                                                @endforeach
                                            @endif
                                        </select>
                                    </td>
                                    <td>
                                        <div class="switch switch-large">
                                            <input type="checkbox" checked :id="index"/>
                                            <input type="hidden" :id="'is_show_'+index" name="is_show[]" value="1"/>
                                        </div>
                                    </td>
                                    <td>
                                        <i class="fa fa-plus" v-if="obj" @click="addList"></i>
                                        <i class="fa fa-minus" v-else  @click="minusList(index)"></i>
                                    </td>
                                </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <!-- /.box-body -->
                    <div class="box-footer">
                        <button type="submit" class="btn btn-primary pull-left">保存</button>
                        <a href="{{ url()->previous() }}" class="btn btn-primary pull-right">返回</a>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <script>
        var app = new Vue({
            el: '#content',
            data: {
                list: [true]
            },
            mounted:function () {
                this.setbootstrapSwitch();
            },
            methods: {
                addList: function () {
                    this.list.push(false);
                    this.$nextTick(function(){
                        this.setbootstrapSwitch();
                        $('select').selectpicker();
                    })
                },
                minusList: function (index) {
                    this.list.splice(index,1)
                },
                setbootstrapSwitch: function () {
                    $('input[type="checkbox"]').bootstrapSwitch({
                        onColor: "success",
                        offColor: "danger",
                        size: "small",
                        onSwitchChange:function(event,state){
                            var id = $(this).attr('id');
                            if(state==true){
                                $($('input[name="is_show[]"]')[id]).val(1);
                            }else{
                                $($('input[name="is_show[]"]')[id]).val(0);
                            }
                        }
                    });
                }
            },
        })
    </script>
@stop